
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;

public class app {

	public static void main(String[] args) throws IOException {
		Long time1 = System.currentTimeMillis();
		Scanner match = new Scanner(new File("query.dat"));
		File string = new File("2.dat");
		RandomAccessFile reader = new RandomAccessFile(string,"r");
		FileWriter write = new FileWriter(new File("output.dat"));
		byte[] line = new byte[(int) string.length()];
		reader.read(line,0,(int) string.length());
		reader.close();
		System.out.println("Running...");
		while (match.hasNext())
		{
			String s = match.nextLine();
			write.write(string.getName()+","+s);
			byte[] target = s.getBytes();
			for(int i=0;i<line.length-target.length+1;i++)
			{
				if(line[i]==target[0])
				{
					int o=0;
					int skip=0;
					boolean found = false;
					for(int j=1;j<target.length;j++)
					{
						if(line[i+j]==target[j])
						{
							if(j==target.length-1)
							{
								write.write(","+i);
								found = true;
							}
						}
						else break;
						if(j+o-1>=0&&j+o-1<line.length)
						{
							if(line[i+j]==target[j+o-1])
							{
								o++;
							}
							else
							{
								skip=skip+o;;
								o=0;
							}
						}
						if(line[i+j]!=target[j]||found==true)
						{
							i=i+skip;
							break;
						}
					}
				}
			}
			write.write("\n");
		}
		System.out.println("End");
		System.out.println((float)(System.currentTimeMillis()-time1)/1000+" s");
		write.close();
		match.close();
	}

}
//1.dat 1399.223s
//2.dat 1.537s
